package se.energimyndigheten.lampguiden.bulbreplacement;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import se.energimyndigheten.lampguiden.LightSource;
import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.XmlClickable;

public class SetPriceDialogFragment extends DialogFragment implements OnSeekBarChangeListener, XmlClickable {

    private static final String TAG = SetPriceDialogFragment.class.getSimpleName();

    private LightSource mLight;
    private int mPrice;
    private SeekBar mSeekBar;
    private TextView mMessagePrice;
    private TextView mMessageSavings;
    private PriceCalculator mCalculator;

    public interface PriceCalculator {
        void onPriceChanged(int price);

        int calculateSavings(int price);
    }

    public static SetPriceDialogFragment newInstance(PriceCalculator pCalc, LightSource light) {
        SetPriceDialogFragment frag = new SetPriceDialogFragment();
        frag.setLightSourceAndPower(light);
        frag.setPriceCalculator(pCalc);
        return frag;
    }

    public void setPriceCalculator(PriceCalculator pCalc) {
        mCalculator = pCalc;
    }

    public void setLightSourceAndPower(LightSource light) {
        mLight = light;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true); // or mLights will be null on orientation
        mPrice = mLight.getPrice();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Activity a = getActivity();
        AlertDialog.Builder ab = new AlertDialog.Builder(a);
        LayoutInflater inf = getActivity().getLayoutInflater();
        View view = inf.inflate(R.layout.setprice_dialog, null);
        TextView mMessageCost = (TextView) view.findViewById(android.R.id.text1);
        mMessagePrice = (TextView) view.findViewById(R.id.dialog_price);
        mMessageSavings = (TextView) view.findViewById(android.R.id.text2);
        mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
        mSeekBar.setOnSeekBarChangeListener(this);
        mSeekBar.setProgress(mPrice);
        ab.setView(view);
        return ab.create();
    }

    /* Control the seek bar to stick at certain steps, like 100 kr instead of 98 kr.   */
    private int stepper(int progress) {
        int fiddle;
        if (progress < 200) {
            // step 5 kr under 200 kr
            int mod = progress % 5;
            if (mod < 3)
                fiddle = -mod;
            else
                fiddle = 5 - mod;
            return progress + fiddle;
        } else {
            // step 10kr
            int mod = progress % 10;
            if (mod < 5)
                fiddle = -mod;
            else
                fiddle = 10 - mod;
            return progress + fiddle;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // Log.d(TAG, "onProgressChanged");
        mPrice = stepper(progress);
        int savings = mCalculator.calculateSavings(mPrice);
        mMessagePrice.setText(getResources().getString(R.string.price_dialog_price, mPrice));
        // special message if savings are below 0 kr
        if (savings > 0) {
            mMessageSavings.setText(getResources().getString(R.string.price_dialog_message2, savings));
        } else {
            mMessageSavings.setText(getResources().getString(R.string.price_dialog_loss));
        }
        seekBar.setContentDescription(mPrice + " kr");
        seekBar.setTag(R.id.seekbar, mPrice);
        seekBar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    }

    @Override
    public void onXmlClick(View view) {
        final int viewId = view.getId();
        switch (viewId) {
            case R.id.change_price_dialog_btn_ok:
                mCalculator.onPriceChanged(mPrice);
                this.dismiss();
                break;
            case R.id.change_price_dialog_btn_reset:
                int price = mLight.getDefaultPrice();
                mSeekBar.setProgress(price);
                mSeekBar.invalidate();
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}
